# coding:utf-8
from bs4 import BeautifulSoup

from baidu_zhidao import html_downloader


class MyTest(object):
    def __init__(self):
        self.downloader = html_downloader.HtmlDownloader()

    def test_down(self, url):
        return self.downloader.download(url)

    def test_ul_soup(self, html_cont):
        soup = BeautifulSoup(html_cont, 'html.parser', from_encoding='utf-8')
        return soup.find_all('ul', id='daily-list')

    def test_get_li(selfself, ul_soup):
        h2_val = ul_soup.find_all('h2')
        for val in h2_val:
            print(val.find('a').get_text())
            print(val.find_parent().find('div', class_='summer').find('a').get_text())
            print(val.find_parent().find('div', class_='summer').find('a').get("href"))

    def test_get_periodical_and_time(self, html_cont):
        soup = BeautifulSoup(html_cont, 'html.parser', from_encoding='utf-8')
        # 期刊
        periodical = soup.find('div', class_='num-wp').get('data-num')
        # 时间
        time = soup.find('span', class_='time').get_text()

        print('期刊 = %s 日期 = %s' % (periodical, time))

    def test_get_title(self, html_cont):
        soup = BeautifulSoup(html_cont, 'html.parser', from_encoding='utf-8')
        banner_title = soup.find('span', class_='banner-title')
        print("title = ", banner_title.get_text())
        print(banner_title.find_parent().find_parent().get('href'))

if __name__ == '__main__':
    root_url = "https://p.baidu.com/daily"
    mTest = MyTest()
    print('start download ------------')
    print('start parser ul ------------')

    html_cont = mTest.test_down(root_url)

    mTest.test_get_periodical_and_time(html_cont)
    mTest.test_get_title(html_cont)
    ul = mTest.test_ul_soup(html_cont)[0]
    mTest.test_get_li(ul)
